
/*
 * GET home page.
 */

var crypto = require('crypto'),
	User = require('../modules/user.js'),
	Post = require('../modules/article.js');

module.exports = function(app){
	app.get('/',function(req,res){
		var name;
		if(req.session.user){
			name = req.session.user.name;
		}
		Post.get(name,function(err,articles){
			if(err){
				articles = [];
			}
			res.render('index', { 
				title: '主页',
				user:req.session.user,
				articles:articles,
				success:req.flash('success').toString(),
				error:req.flash('error').toString()
			});
		});
	});
	app.get('/reg',checkNotLogin);
	app.get('/reg',function(req,res){
		res.render('reg',{ title: '注册'});
	});
	app.post('/reg',checkNotLogin);
	app.post('/reg',function(req,res){
		var name = req.body.name,
			password = req.body.password,
			password_re = req.body['password-repeat'];
		if(password_re != password){
			req.flash('error','两次输入的密码不一致！');
			return res.redirect('/reg');
		}
		//生成密码的散列值
		var md5 = crypto.createHash('md5'),
			password = md5.update(req.body.password).digest('hex');
		var newUser = new User({
			name:name,
			password:password,
			email:req.body.email
		});
		//检查用户名是否已经存在
		User.get(newUser.name,function(err,user){
			if(user){
				err = '用户已存在!';
			}
			if(err){
				req.flash('error',err);
				return res.redirect('/reg');
			}
			//如果不存在则新增用户
			newUser.save(function(err){
				if(err){
					req.flash('error',err);
					return res.redirect('/reg');
				}
				req.session.user = newUser;//用户信息存入session
				req.flash('success','注册成功！');
				res.redirect('/');
			});
		});
	});
	app.get('/login',checkNotLogin);
	app.get('/login',function(req,res){
		res.render('login',{
			title:'登录',
			user: req.session.user,
			success:req.flash('success').toString(),
			error:req.flash('error').toString()
		});
	});
	app.post('/login',checkNotLogin);
	app.post('/login',function(req,res){
		//生成密码的散列值
		var md5 = crypto.createHash('md5'),
			password = md5.update(req.body.password).digest('hex');
		//检查用户是否存在
		User.get(req.body.name,function(err,user){
			if(!user){
				req.flash('error','用户不存在');
				return res.redirect('/login');
			}
			//检查密码产否一致
			if(user.password != password){
				req.flash('error','密码错误！');
				return res.redirect('/login');
			}
			//用户名密码都匹配后，将用户信息存入session
			req.session.user = user;
			req.flash('success','登录成功！');
			res.redirect('/');
		});
	});
	app.get('/article', checkLogin);
	app.get('/article',function(req,res){
		res.render('article',{
			title:'发表',
			user:req.session.user,
			success:req.flash('success').toString(),
			error:req.flash('error').toString()
		});
	});
	app.post('/article', checkLogin);
	app.post('/article',function(req,res){
		var currentUser = req.session.user,
			article = new Article(currentUser.name,req.body.title,req.body.articleType,req.body.article);
		article.save(function(err){
			if(err){
				req.flash('error',err);
				return res.redirect('/');
			}
			req.flash('success','发布成功');
			res.redirect('/');
		});
	});
	app.get('/logout', checkLogin);
	app.get('/logout',function(req,res){
		req.session.user = null;
		req.flash('success','登出成功！');
		res.redirect('/');
	});
	app.post('/logout',checkLogin);
	app.post('/logout',function(req,res){
		
	});
};

function checkLogin(req,res,next){
	if(!req.session.user){
		req.flash('error','未登录');
		return res.redirect('/login');
	}
	next();
}

function checkNotLogin(req,res,next){
	if(req.session.user){
		req.flash('error','已登录');
		return res.redirect('/login');
	}
	next();
}
